//
//  MXHomeItemDetailTopView.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/10.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//

#import "MXHomeItemDetailTopView.h"
#import "UIImageView+WebCache.h"

@interface MXHomeItemDetailTopView ()
@property (nonatomic, retain) UIView *line;
@property (nonatomic, retain) UILabel *oldPricelabel;
@property (nonatomic, retain) UILabel *priceWordLabel;
@property (nonatomic, assign, getter=haveSize) BOOL haveSize;
@end
@implementation MXHomeItemDetailTopView
- (void)dealloc
{
    [_priceWordLabel release];
    [_oldPricelabel release];
    [_line release];
    [_ItemModelS release];
    [_mainImageView release];
    [_priceLabel release];
    [_describeLabel release];
    [_recommendLabel release];
    [_summaryLabel release];
    [super dealloc];
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.mainImageView = [[[UIImageView alloc] init] autorelease];
        [self addSubview:self.mainImageView];
        
        self.describeLabel = [[[UILabel alloc] init] autorelease];
        self.describeLabel.backgroundColor = [UIColor cyanColor];
        self.describeLabel.alpha = 0.6;
        self.describeLabel.numberOfLines = 2;
        [self addSubview:self.describeLabel];
        
        self.priceLabel = [[[UILabel alloc] init] autorelease];
        self.priceLabel.text = @"";
        self.priceLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.priceLabel];
        
        self.oldPricelabel = [[[UILabel alloc] init] autorelease];
        self.oldPricelabel.backgroundColor = [UIColor whiteColor];
        self.oldPricelabel.text = @"";
        self.oldPricelabel.textAlignment = NSTextAlignmentCenter;
        [self.oldPricelabel setTextColor:[UIColor grayColor]];
        self.oldPricelabel.font = [UIFont systemFontOfSize:12];
        [self.priceLabel addSubview:self.oldPricelabel];
        
        self.line = [[[UIView alloc] init] autorelease];
        self.line.backgroundColor = [UIColor grayColor];
        [self.oldPricelabel addSubview:_line];
        
        self.recommendLabel = [[[UILabel alloc] init] autorelease];
        self.recommendLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.recommendLabel];
        
        self.summaryLabel = [[[UILabel alloc] init] autorelease];
        self.summaryLabel.backgroundColor = [UIColor whiteColor];
        self.summaryLabel.textAlignment = NSTextAlignmentCenter;
        self.summaryLabel.layer.borderColor = [UIColor blackColor].CGColor;
        self.summaryLabel.layer.borderWidth = 0.3;
        [self.summaryLabel setTextColor:[UIColor blueColor]];
        self.summaryLabel.text = @"产品信息";
        [self addSubview:self.summaryLabel];
        
        self.priceWordLabel = [[[UILabel alloc] init] autorelease];
        self.priceWordLabel.text = @"价格";
        [self.priceLabel addSubview:self.priceWordLabel];
        
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat WID = self.frame.size.width;
//    CGFloat margin = 20;
    
    CGFloat MainImageX = 0;
    CGFloat MainImageY = 0;
    CGFloat MainImageW = WID;
    CGFloat MainImageH = HEIGHTPRO * 400;
    self.mainImageView.frame = CGRectMake(MainImageX, MainImageY, MainImageW, MainImageH);
    
    self.describeLabel.frame = CGRectMake(MainImageX, MainImageH - HEIGHTPRO * 50, WID, HEIGHTPRO * 50);
    
    CGFloat priceLabelX = 0;
    CGFloat priceLabelY = MainImageH;
    CGFloat priceLabelW = WID;
    CGFloat priceLabelH = HEIGHTPRO * 60;
    self.priceLabel.frame = CGRectMake(priceLabelX, priceLabelY, priceLabelW, priceLabelH);
    
    self.priceWordLabel.frame = CGRectMake(0, 0, HEIGHTPRO * 60, priceLabelH);
    
    self.oldPricelabel.frame = CGRectMake(WIDTHPRO * 95, HEIGHTPRO * 15, WIDTHPRO * 60, HEIGHTPRO * 30);
    
    self.line.frame = CGRectMake(WIDTHPRO * 0 , HEIGHTPRO * 15, WIDTHPRO * 60, HEIGHTPRO * 1);
    
    if (self.haveSize == YES) {
        CGFloat recommendLabelX = 0;
        CGFloat recommendLabelY = priceLabelH + priceLabelY;
        CGFloat recommendLabelW = WID;
        CGFloat recommendLabelH = HEIGHTPRO * 40;
        self.recommendLabel.frame = CGRectMake(recommendLabelX, recommendLabelY, recommendLabelW, recommendLabelH);
        CGFloat summarylabelX = 0;
        CGFloat summarylabelY = recommendLabelH + recommendLabelY + HEIGHT / 667 * 15;
        CGFloat summarylabelW = WID;
        CGFloat summarylabelH = HEIGHTPRO * 60;
        self.summaryLabel.frame = CGRectMake(summarylabelX, summarylabelY, summarylabelW, summarylabelH);
    } else {
        
        
        CGFloat summarylabelX = 0;
        CGFloat summarylabelY = priceLabelH + priceLabelY + HEIGHTPRO * 15;
        CGFloat summarylabelW = WID;
        CGFloat summarylabelH = HEIGHTPRO * 60;
        self.summaryLabel.frame = CGRectMake(summarylabelX, summarylabelY, summarylabelW, summarylabelH);
    }

}

- (void)GetInfo:(MXHomeItemDetailModel *)model
{
    if (_ItemModelS != model) {
        [_ItemModelS release];
        _ItemModelS = [model retain];
    }
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"plaseholder"]];
    
    self.describeLabel.text = model.title;
    
    if (model.size) {
        self.haveSize = YES;
        
        NSInteger lenght = model.size.length;
        
        NSMutableAttributedString *recommendStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"小编推荐 衣服尺码:%@, 仅供参考", model.size]];
        [recommendStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 4)];
        [recommendStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, 4)];
        [recommendStr addAttribute:NSForegroundColorAttributeName value:[UIColor cyanColor] range:NSMakeRange(10, lenght)];
        self.recommendLabel.attributedText = recommendStr;
        self.recommendLabel.textAlignment = NSTextAlignmentCenter;
    } else {
        self.recommendLabel.hidden = YES;
    }
    
    
    
#pragma mark - 在一行字符串中设置不同的颜色
    
//    NSMutableAttributedString *priceStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"价格:   ¥%@  ¥%@  ",model.coupon_price, model.price]];
//    [priceStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 2)];
//    [priceStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,7)];
//    [priceStr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(13, 8)];
//    [priceStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(13, 8)];
    
    CGFloat price = model.coupon_price.floatValue;
    CGFloat oldPrice = model.price.floatValue;
    
    self.priceLabel.text = [NSString stringWithFormat:@"         ¥%.0f", price];
    [self.priceLabel setTextColor:[UIColor redColor]];
    
    self.oldPricelabel.text = [NSString stringWithFormat:@"¥%.0f", oldPrice];

    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
